package com.woniuxy.woniubook.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CancelOrderRabbitConfig {
    //1. 创建 普通交换机
    @Bean
    public DirectExchange cancelOrderExchange(){
        return new DirectExchange("cancelOrderExchange");
    }
    //2 创建一个 死信交换机
    @Bean
    public DirectExchange deadExchange(){
        return new DirectExchange("deadExchange");
    }
    //3. 创建一个 普通队列 （ttl  deadExchange ，routingKey）
    @Bean
    public Queue orderQueue(){
        return QueueBuilder.durable("orderQueue")
                .deadLetterExchange("deadExchange")
                .deadLetterRoutingKey("dead_routing_key")
                .ttl(5*60*1000)
                .build();
    }
    //绑定 普通交换机和 普通队列
    @Bean
    public Binding bindDirectExchangeAndQueueA(){
        return BindingBuilder.bind(orderQueue()).to(cancelOrderExchange()).with("order_routing_key");
    }
    //创建一个死信队列
    @Bean
    public Queue deadQueue(){
        return QueueBuilder.durable("deadQueue").build();
    }

    //绑定死信交换机 和 死信队列
    @Bean
    public Binding bindDeadExchangeAndDeadQueue(){
        return BindingBuilder.bind(deadQueue()).to(deadExchange()).with("dead_routing_key");
    }
}
